home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Printing / PostScriptHandleDemo / PSHdlDemo.p < prev    next >
Encoding:
Text File  |  1992-10-06  |  4.5 KB  |  226 lines  |  [TEXT/MPS ]

  1. {**
  2.  **     Program: PSHdlDemo
  3.  **
  4.  **     Version: 1.0    3/31/92
  5.  **
  6.  **        MPW 3.2 Pascal source.
  7.  **
  8.  **     Purpose:
  9.  **
  10.  **        PSHdlDemo demonstrates how to use PostScriptHandle to send PostScript from a text
  11.  **        file to the printer.
  12.  **        
  13.  **
  14.  **        - Dave Hersey
  15.  **
  16.  **        Macintosh Developer Technical Support
  17.  **
  18.  **
  19.  **}
  20.  
  21. PROGRAM PSHdlDemo;
  22.  
  23. USES
  24.     MemTypes, QuickDraw, OSIntf, ToolIntf, Traps, MacPrint, Packages, PrintComments;
  25.  
  26.  
  27. {*------ SendPostScript ------------------------------------------------------------*}
  28.  
  29. PROCEDURE SendPostScript(theBuffer: Ptr; buffSize : Longint);
  30. VAR
  31.     dataHdl        : Handle;
  32.     err            : OSErr;
  33.     
  34. BEGIN
  35.     
  36.     err := PtrToHand(theBuffer, dataHdl, buffSize);
  37.  
  38.     IF (err = noErr) THEN
  39.     BEGIN
  40.         PicComment(PostScriptHandle, buffSize, dataHdl);
  41.         DisposHandle(dataHdl);
  42.     END;
  43. END;
  44.  
  45.  
  46. {*------ DrawStuff -----------------------------------------------------------------*}
  47.  
  48. {**
  49.  **      DrawStuff will open up a text document and send it to the printer
  50.  **      as PostScript.  Hopefully, the text WILL BE PostScript!
  51.  **}
  52.  
  53.  PROCEDURE DrawStuff(theFile: SFReply);
  54.  
  55.  CONST
  56.     fourK    = 4 * 1024;
  57.  
  58.  VAR
  59.        bytesToRead    , amtToRead    : Longint;
  60.     theBuffer                : Ptr;
  61.     err                        : OSErr;
  62.     refNum                    : Integer;
  63.    
  64.        postStr                    : Str255;
  65.    
  66.  BEGIN
  67.  
  68.     { Set up a clipping region.  Unless we do some QuickDraw, no clipping region will
  69.       be set up and everything will be clipped, giving us a blank page. }
  70.  
  71.     PenSize(0, 0);
  72.     MoveTo(0, 0); 
  73.     Line(0, 0);
  74.     PenSize(1, 1);
  75.     
  76.     { Set up the normal PostScript translation/rotation factors.  the Printing Manager
  77.       changes these to match QuickDraw, and if we're just using straight PostScript,
  78.       that will mess us up.}
  79.  
  80.     postStr := '0 728 translate 1 -1 scale ';
  81.     SendPostScript(@postStr[1], Longint(postStr[0]));
  82.  
  83.  
  84.     { Open our file, and determine its length. }
  85.  
  86.     err := FSOpen(theFile.fName, theFile.vRefNum, refNum);
  87.  
  88.     IF (err = noErr) THEN
  89.         err := GetEOF(refNum, bytesToRead);
  90.  
  91.     
  92.     { Read in its contents and send it to the printer using PostScriptHandle.
  93.       We read the data in in 4K blocks, because the driver will break it
  94.       down to that size anyway. }
  95.  
  96.  
  97.     theBuffer := NewPtr(fourK);
  98.     
  99.     WHILE ((err = noErr) AND (bytesToRead <> 0)) DO
  100.     BEGIN
  101.         
  102.         IF bytesToRead > fourK THEN
  103.             amtToRead := fourK
  104.         ELSE
  105.         BEGIN
  106.             amtToRead := bytesToRead;
  107.             SetPtrSize(theBuffer, amtToRead);
  108.         END;
  109.         
  110.         err := FSRead(refNum, amtToRead, theBuffer);
  111.         
  112.         IF (err = noErr) THEN
  113.         BEGIN
  114.             bytesToRead := bytestoRead - amtToRead;
  115.             SendPostScript(theBuffer, amtToRead);
  116.         END;
  117.     
  118.     END;
  119.     
  120.     err := FSClose(refNum);
  121.     
  122.  END;  {**  DrawStuff  **}
  123.  
  124.  
  125.  
  126.  
  127. {*------ PrintStuff ----------------------------------------------------------------*}
  128. {**
  129.  **        PrintStuff gets a text file to print (as PostScript) from the user and then
  130.  **        makes all the print calls to print it.
  131.  **}
  132.  
  133. PROCEDURE PrintStuff;
  134.  
  135.     VAR
  136.         numCopies, copy        : Integer;
  137.         PrintError            : LongInt;
  138.         oldPort              : GrafPtr;
  139.         thePrRecHdl            : THPrint;
  140.         thePrPort            : TPPrPort;
  141.         theStatus            : TPrStatus;
  142.         theFile                : SFReply;
  143.         textTypes            : SFTypeList;
  144.         where                : Point;
  145.     
  146. BEGIN
  147.  
  148.     GetPort(oldPort);
  149.     
  150.     SetPt(where, 100, 100);
  151.     textTypes[0] := 'TEXT';
  152.     SFGetFile(where, '', nil, 1, textTypes, nil, theFile);
  153.     
  154.     thePrRecHdl := THPrint(NewHandle(SIZEOF(TPrint)));
  155.     
  156.     IF (theFile.good) AND (MemError = noErr) AND (thePrRecHdl <> nil) THEN
  157.     BEGIN
  158.         PrOpen;
  159.  
  160.         IF (PrError = noErr) THEN
  161.         BEGIN
  162.             PrintDefault(thePrRecHdl);
  163.  
  164.             IF (PrError = noErr) THEN
  165.             BEGIN
  166.                IF PrStlDialog(thePrRecHdl) THEN
  167.                 BEGIN
  168.                     IF PrJobDialog(thePrRecHdl) THEN 
  169.                     BEGIN
  170.                         
  171.                         numCopies := thePrRecHdl^^.prJob.iCopies;
  172.  
  173.                         FOR copy := 1 TO numCopies DO
  174.                         BEGIN
  175.                         
  176.                             thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
  177.                         
  178.                             IF (PrError = noErr) THEN
  179.                             BEGIN
  180.     
  181.                                 PrOpenPage(thePrPort, nil);
  182.                                     
  183.                                 IF (PrError = noErr) THEN
  184.                                     DrawStuff(theFile);
  185.                                      
  186.                                 PrClosePage(thePrPort);
  187.                                 
  188.                             END;
  189.                                  
  190.                             PrCloseDoc(thePrPort);
  191.  
  192.                         END;
  193.                         
  194.                         IF (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) THEN
  195.                             PrPicFile(thePrRecHdl, nil, nil, nil, theStatus);
  196.  
  197.                     END;
  198.                 END;
  199.             END;
  200.         END;
  201.         
  202.         PrClose;
  203.  
  204.     END;
  205.      
  206.     IF (thePrRecHdl <> nil) THEN DisposHandle(Handle(thePrRecHdl));
  207.  
  208. END;  {**  PrintStuff  **}
  209.  
  210.  
  211. {*------ main ----------------------------------------------------------------------*}
  212.  
  213. BEGIN
  214.     
  215.     InitGraf(@thePort);
  216.     InitFonts;
  217.     FlushEvents(everyEvent, 0);    
  218.     InitWindows;
  219.     InitMenus;
  220.     TEInit;
  221.     InitDialogs(nil);
  222.     InitCursor;
  223.  
  224.     PrintStuff;
  225.  
  226. END. {**  main  **}